﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SalaryMS
{
    public partial class SalaryVerifyUI : Form
    {
        public SalaryVerifyUI()
        {
            InitializeComponent();

            txtMoney.Enabled = false;
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            lblBankAccount.Text = "";
            lblCurrentMoney.Text = "";
            lblEmpName.Text = "";
            lblSalaryDate.Text = "";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            String ID = txtID.Text.Trim().ToString();
            if (ID.Length > 0)
            {
                String sql = "SELECT SalaryDate,BankAccount,Money,EmpName FROM Salary WHERE Id='"+ID+"'";
                DataTable dt = DBSession.ExecuteDataTable(sql, null);
                if (dt != null && dt.Rows.Count > 0)
                {
                    lblBankAccount.Text = dt.Rows[0]["BankAccount"].ToString();
                    lblCurrentMoney.Text = dt.Rows[0]["Money"].ToString();
                    lblEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                    lblSalaryDate.Text = dt.Rows[0]["SalaryDate"].ToString();
                    txtMoney.Enabled = true;
                    btnModify.Enabled = true;
                    btnDelete.Enabled = true;
                    this.AcceptButton = btnModify;
                    txtMoney.Focus();
                }
                else
                {
                    MessageBox.Show("记录ID号不存在，请重新输入！");
                    txtID.Focus();
                    txtMoney.Enabled = false;
                    btnModify.Enabled = false;
                    btnDelete.Enabled = false;
                    lblBankAccount.Text = "";
                    lblCurrentMoney.Text = "";
                    lblEmpName.Text = "";
                    lblSalaryDate.Text = "";
                }
            }
            else
            {
                MessageBox.Show("记录ID号不能为空！");
                txtID.Focus();
                txtMoney.Enabled = false;
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                lblBankAccount.Text = "";
                lblCurrentMoney.Text = "";
                lblEmpName.Text = "";
                lblSalaryDate.Text = "";
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (txtMoney.Text.Trim().Length > 0)
            {
                Decimal menoy = 0;
                try
                {
                    menoy = Decimal.Round(Decimal.Parse("" + txtMoney.Text.Trim() + " "), 2);
                    String sql = "UPDATE Salary SET Money='"+menoy.ToString("#0.00")+"' WHERE Id='"+txtID.Text.Trim().ToString()+"'";
                    int num = DBSession.ExecuteNonQuery(sql, null);
                    if (num > 0)
                    {
                        txtID.Focus();
                        txtMoney.Enabled = false;
                        btnModify.Enabled = false;
                        btnDelete.Enabled = false;
                        lblBankAccount.Text = "";
                        lblCurrentMoney.Text = "";
                        lblEmpName.Text = "";
                        lblSalaryDate.Text = "";
                        txtMoney.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("修改数据失败，请重试！");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("输入金额格式出错，请重输！");
                }
            }
            else
            {
                MessageBox.Show("金额不能为空！");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            String sql = "DELETE FROM Salary WHERE Id='"+ txtID.Text.Trim().ToString() + "'";
            int num = DBSession.ExecuteNonQuery(sql, null);
            if (num > 0)
            {
                MessageBox.Show("删除记录成功！");
                txtID.Focus();
                txtMoney.Enabled = false;
                btnModify.Enabled = false;
                btnDelete.Enabled = false;
                lblBankAccount.Text = "";
                lblCurrentMoney.Text = "";
                lblEmpName.Text = "";
                lblSalaryDate.Text = "";
            }
            else
            {
                MessageBox.Show("删除失败，请重试！");
            }
        }
    }
}
